package com.joysuch.wwyt.api.mh.controller;

import com.joysuch.wwyt.api.mh.bean.MhPlatformBean;
import com.joysuch.wwyt.api.mh.bean.MhPlatformFieldBean;
import com.joysuch.wwyt.api.mh.bean.MhPlatformPointBean;
import com.joysuch.wwyt.api.mh.entity.MhPlatform;
import com.joysuch.wwyt.api.mh.entity.MhPlatformField;
import com.joysuch.wwyt.api.mh.entity.MhPlatformPoint;
import com.joysuch.wwyt.api.mh.service.MhPlatformDataDockingService;
import com.joysuch.wwyt.api.mh.service.MhPlatformSocketService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;

@RestController
@RequestMapping("/mh/api-platform-data")
@Api(tags = "/mh/api-platform-data 平台数据对接")
public class MhPlatformDataDockingController {

    @Autowired
    private MhPlatformDataDockingService mhPlatformDataDockingService;
    @Autowired
    private MhPlatformSocketService socketService;

    @PostMapping("/addPlatform")
    @ApiOperation("增加平台")
    @RequiresPermissions("mh:api-platform-data:addPlatform")
    public ResultBean addPlatform(@RequestBody MhPlatformBean bean) {
        MhPlatform f = mhPlatformDataDockingService.addPlatform(bean);
        return ResultBean.success(f);
    }

    @PostMapping("/updatePlatform")
    @ApiOperation("修改平台")
    @RequiresPermissions("mh:api-platform-data:updatePlatform")
    public ResultBean updatePlatform(@RequestBody MhPlatformBean bean) {
        MhPlatform f = mhPlatformDataDockingService.updatePlatform(bean);
        return ResultBean.success(f);
    }

    @GetMapping("/getPlatformById")
    @ApiOperation("查看平台")
    public ResultBean getPlatformById(Long id) {
        MhPlatformBean bean = mhPlatformDataDockingService.getPlatformById(id);
        return ResultBean.success(bean);
    }

    @GetMapping("/getPlatformList")
    @ApiOperation("获取平台列表")
    public ResultBean getPlatformList() {
        List<MhPlatformBean> list = mhPlatformDataDockingService.getPlatformList();
        return ResultBean.success(list);
    }

    @GetMapping("/deletePlatformById")
    @ApiOperation("删除平台")
    @RequiresPermissions("mh:api-platform-data:deletePlatformById")
    public ResultBean deletePlatformById(Long id) {
        mhPlatformDataDockingService.deletePlatformById(id);
        return ResultBean.success("");
    }

    @PostMapping("/addPoint")
    @ApiOperation("增加平台监测点")
    @RequiresPermissions("mh:api-platform-data:addPoint")
    public void addPoint(@RequestBody List<MhPlatformPointBean> list) {
        mhPlatformDataDockingService.addPoint(list);
    }

    @PostMapping("/editPoint")
    @ApiOperation("修改平台监测点")
    @RequiresPermissions("mh:api-platform-data:addPoint")
    public ResultBean editPoint(@RequestBody MhPlatformPointBean bean) {
        MhPlatformPoint p = mhPlatformDataDockingService.editPoint(bean);
        return ResultBean.success(p);
    }

    @GetMapping("/deletePointById")
    @ApiOperation("删除平台监测点")
    @RequiresPermissions("mh:api-platform-data:addPoint")
    public ResultBean deletePointById(Long id) {
        mhPlatformDataDockingService.deletePointById(id);
        return ResultBean.success("");
    }

    @GetMapping("/getPointById")
    @ApiOperation("查看平台监测点")
    public ResultBean getPointById(Long id) {
        MhPlatformPointBean bean = mhPlatformDataDockingService.getPointById(id);
        return ResultBean.success(bean);
    }

    @GetMapping("/pagePoint")
    @ApiOperation("平台监测点分页")
    public ResultBean pagePoint(Pageable page, Long platformId, String codeOrName) {
        return mhPlatformDataDockingService.pagePoint(page, platformId, codeOrName);
    }

    @PostMapping("/saveField")
    @ApiOperation("保存平台字段")
    @RequiresPermissions("mh:api-platform-data:saveField")
    public ResultBean saveField(@RequestBody MhPlatformFieldBean bean) {
        MhPlatformField f = mhPlatformDataDockingService.saveField(bean);
        return ResultBean.success(f);
    }

    @GetMapping("/deleteFieldById")
    @ApiOperation("删除平台字段")
    @RequiresPermissions("mh:api-platform-data:deleteFieldById")
    public ResultBean deleteFieldById(Long id) {
        mhPlatformDataDockingService.deleteFieldById(id);
        return ResultBean.success("");
    }

    @GetMapping("/getFieldLIstById")
    @ApiOperation("获取平台字段列表")
    public ResultBean getFieldLIstById(Long id) {
        List<SimpleSelectListBean> list = mhPlatformDataDockingService.getFieldLIstById(id);
        return ResultBean.success(list);
    }

    @GetMapping("/getData")
    @ApiOperation("获取数据")
    public ResultBean getData(Long id) {
        return mhPlatformDataDockingService.getData(id);
    }

    @GetMapping("/transferMhPointBySocket")
    @ApiOperation("接入监测点")
    public void transferMhPointBySocket(Long platformId) {
        socketService.transferMhPointBySocket(platformId);
    }
}
